package com.xwwx.tag;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xwwx.common.Constant;

/**
 * 功能:验证用户是否合法
 * @author liwei
 * @since 2010-06-04
 */
@SuppressWarnings("serial")
public class ValidateSessionTag extends TagSupport{

	@SuppressWarnings("unused")
	private final Log logger = LogFactory.getLog(ValidateSessionTag.class);

	public int doEndTag() throws JspException {
		
		String page = Constant.UNLOGON_JSP;
		boolean valid = false;
		HttpSession session = pageContext.getSession();
		
		if ((session != null)) {
			if (session.getAttribute(Constant.USER_SESSION_KEY) != null) {
				valid = true;
			}
		}
		if (valid)
			return (EVAL_PAGE);
		else {
			try {
				pageContext.forward(page);
			} catch (Exception e) {
				throw new JspException(e.toString());
			}
			return (SKIP_PAGE);
		}
	}

	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

}
